<template>
	<om-page :title="pageTitle">
		<om-formpage :hasSearch="true" :datas="search?searchData:commonData.datas" :userid="userId" @btnClick="btnClick"
			@confirm="confirm">
		</om-formpage>
		<uni-fab horizontal="right" @trigger="trigger" direction="vertical" :content="buttons" :scrollTop="scrollTop" />
		<tui-loadmore :visible="commonData.loadding"></tui-loadmore>
		<tui-nomore :visible="!commonData.pullUpOn"></tui-nomore>
	</om-page>
</template>

<script>
	import {
		confirmModal
	} from "../../../static/js/common.js"
	import listLoad from '../../../static/js/listLoad.js';
	import {
		mapState,
		mapActions
	} from 'pinia';
	import {
		useLoginUserStore
	} from '@/stores/login-user.js'
	const app = getApp();
	export default {
		data() {
			return {
				searchStr: '',
				pageTotal: 1,
				pageNum: 1,
				pageSize: 20,
				scrollTop: 0,
				buttons: [app.globalData.button.read, app.globalData.button.home],
				commonData: {
					datas: []
				},
				pageTitle: "",
				str: [],
				search: false,
				searchData: [],
				value: ""
			};
		},
		watch: {
			commonData: {
				handler(newVal, oldVal) {
					if (newVal.datas.length) {
						for (let item of newVal.datas) {
							item.isShowButton = true
						}
					}
				},
				deep: true,
				immediate: true
			}
		},
		computed: mapState(useLoginUserStore,['userId']),
		methods: {
			...mapActions(useLoginUserStore,[
				'setUnread'
			]),
			confirm(inputVal) {
				let _that = this
				_that.searchStr = inputVal.value
				_that.pageNum = 1
				_that.commonData.datas = []
				_that.getNotifyList()
				// this.value = inputVal
				// _that.searchData = _that.commonData.datas.filter((e,i)=>{
				// 	if(e.title.indexOf(inputVal.value) >-1){
				// 		return e
				// 	}
				// 	if(e.userName.indexOf(inputVal.value) >-1){
				// 		return e
				// 	}
				// 	if(e.code.indexOf(inputVal.value) >-1){
				// 		return e
				// 	}
				// })
				// _that.search = true
			},
			btnClick(e) {
				let _that = this
				let deleteArr = []
				let newArr = _that.commonData.datas.filter((item, i) => {
					if (e.formCode == item.formCode && e.fileId == item.fileId) {
						_that.om.request(
							"flowNotifyConfig/readOne", {
								formCode: item.formCode,
								fileId: item.fileId,
							},
							"post"
						).then(res => {})
					} else {
						return item
					}
				})
				_that.commonData.datas = newArr
				if (_that.value) {
					obj.value = _that.value
					_that.confirm(obj)
				}
				_that.setUnread(true)
			},
			allRead() {
				let _that = this
				_that.om.request(
					"flowNotifyConfig/allRead", {
						str: this.str
					},
					"post"
				).then(res => {
					_that.searchStr = ''
					_that.pageNum = 1
					_that.commonData.datas = []
					_that.getNotifyList()
					// listLoad.initData(this, 'wxCommon/getNotifyList');
					this.setUnread(true)
				})
			},
			trigger(e) {
				if (e.item.text === '返回首页') {
					uni.switchTab({
						url: '/pages/main/main/main'
					});
				} else if (e.item.text === '返回顶部') {
					this.scrollTop = 0;
					uni.pageScrollTo({
						scrollTop: 0
					});
				} else if (e.item.text === "全部已读") {
					if (this.commonData.datas.length) {
						confirmModal(this, "提示信息", "确定将未读消息全部已读？", () => {
							this.allRead()
						}, () => {})
					} else {
						this.om.toast("无未读信息")
					}
				}
			},
			// searchClick(e) {
			// 		let _that = this;
			// 		_that.initData(this,'wxCommon/getNotifyList',{
			// 		zuserName: e
			// 	});
			// },
			getNotifyList() {
				let _that = this;
				_that.om.request("init/getNotifyData", {
					page: _that.pageNum,
					rows: _that.pageSize,
					searchStr: _that.searchStr
				}).then(res => {
					_that.commonData.datas = _that.commonData.datas.concat(res.rows)
					for (let i = 0; i < _that.commonData.datas.length; i++) {
						_that.commonData.datas[i].code = _that.commonData.datas[i].fileCode
						_that.commonData.datas[i].userId = _that.commonData.datas[i].zuserId
						_that.commonData.datas[i].userName = _that.commonData.datas[i].zuserName
						_that.commonData.datas[i].startdate = _that.commonData.datas[i].ztime
					}
					uni.setStorageSync('pendingListTotal', res.total)
					_that.pageTotal = res.total

				})
			}
		},
		onLoad: function(options) {
			let _that = this;
			this.pageTitle = (options.title || '未阅读')
			uni.setNavigationBarTitle({
				title: this.pageTitle
			});
		},
		onShow() {
			this.pageNum = 1
			this.commonData.datas = []
			this.getNotifyList()
		},
		onPageScroll(e) {
			let _that = this;
			_that.scrollTop = e.scrollTop;
			let buttons = _that.buttons;
			if (e.scrollTop > 5) {
				if (_that.buttons.length === 2) {
					buttons.splice(1, 0, app.globalData.button.top);
					_that.buttons = buttons;
				}
			} else {
				if (_that.buttons.length === 3) {
					buttons.splice(1, 1);
					_that.buttons = buttons;
				}
			}
		},
		//页面相关事件处理函数--监听用户下拉动作
		// onPullDownRefresh: function() {
		// 	listLoad.pullDownRefresh(this);
		// },

		// 页面上拉触底事件的处理函数
		onReachBottom: function() {
			let _that = this;
			this.commonData.pullUpOn = false
			if (_that.pageTotal > _that.pageNum * _that.pageSize) {
				_that.pageNum++
				_that.getNotifyList()
			}
			return
		}
	};
</script>

<style></style>